﻿using FastCloud.Base.Config;
using FastCloud.Base.DB;
using FastCloud.Base.Desgin;
using FastCloud.Base.Entity;
using FastIOC.Attribute;
using FastUtil.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Base.Realization
{
    [Service(RegistName = "DB")]
    public class DataBaseAttachStorage : IAttachStorage
    {
        [AutoWired]
        private DB_AttachConfig DB_AttachConfig;
        [AutoWired]
        private DB_AttachStorage DB_AttachStorage;
        [AutoWired]
        private DB_AttachInfo DB_AttachInfo;
        public virtual void DeleteAttach(string AttachGuid)
        {
            Frame_AttachInfo AttachInfo = DB_AttachInfo.SelectOne(AttachGuid);
            DB_AttachStorage.DeleteByID(AttachInfo.AttachStorageGuid);
        }

        public virtual string GetAttachUrl(string AttachGuid)
        {
            Frame_AttachInfo AttachInfo = DB_AttachInfo.SelectOne(AttachGuid);
            string ConnectString = CloudUtil.GetBaseConnectString();
            Frame_AttachConfig AttachConfig = DB_AttachConfig.SelectEnableConfig();
            if (null != AttachConfig)
            {
                ConnectString = AttachConfig.ConfigConnectionString;
            }
            DB_AttachStorage.ChangeConnectString(ConnectString);
            Frame_AttachStorage AttachStorage = DB_AttachStorage.SelectOne(AttachInfo.AttachStorageGuid);
            //DB附件暂存本地
            string TempPath = CloudUtil.GetAttachTempPath() + "/";
            string AbsoulateTempPath = CloudUtil.GetContentPath() + "/" + TempPath;
            DirectoryInfo TempDir = new DirectoryInfo(AbsoulateTempPath);
            if (!TempDir.Exists)
            {
                TempDir.Create();
            }
            string TempAttachPath = CloudUtil.GetAttachTempPath() + "/"+ AttachGuid+"/";
            string AbsoulateTempAttachPath = CloudUtil.GetContentPath() + "/" + TempAttachPath;
            DirectoryInfo TempAttachDir = new DirectoryInfo(AbsoulateTempAttachPath);
            if (!TempAttachDir.Exists)
            {
                TempAttachDir.Create();
            }
            string VirtualPath = TempAttachPath + "/" + AttachInfo.AttachFileName;
            string AbsoulateVirtualPath = CloudUtil.GetContentPath() + "/" + VirtualPath;
            if (!File.Exists(AbsoulateVirtualPath))
            {
                FileUtil.SaveFile(AbsoulateVirtualPath, AttachStorage.Content);
            }
            return CloudUtil.GetRootPath() + VirtualPath;
        }

        public virtual void InsertAttach(Frame_AttachInfo AttachInfo, byte[] AttachContent)
        {
            string ConnectString = CloudUtil.GetBaseConnectString();
            Frame_AttachStorage AttachStorage = new Frame_AttachStorage();
            AttachStorage.AttachFileName = AttachInfo.AttachFileName;
            AttachStorage.FileSize = AttachInfo.FileSize;
            AttachStorage.GroupGuid = AttachInfo.GroupGuid;
            AttachStorage.GroupType = AttachInfo.GroupGuid;
            AttachStorage.Content = AttachContent;
            Frame_AttachConfig AttachConfig = DB_AttachConfig.SelectEnableConfig();
            if (null!= AttachConfig)
            {
                AttachInfo.ConfigName = AttachConfig.ConfigName;
                DB_AttachInfo.UpdateByID(AttachInfo);
                ConnectString = AttachConfig.ConfigConnectionString;
            }
            DB_AttachStorage.ChangeConnectString(ConnectString);
            DB_AttachStorage.Insert(AttachStorage);
            AttachInfo.AttachStorageGuid = AttachStorage.AttachStorageGuid;
            DB_AttachInfo.UpdateByID(AttachInfo);
        }
    }
}
